package de.uni_mannheim.informatik.ai.dame_ai.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Tag{
	private String name;
	private Map<String, String> attributes=new HashMap<String, String>();
	private boolean isClosingTag;
	int id=-1;
	
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}
	
	public String getAtribute(String name){
		return this.attributes.get(name);
	}

	public String getName() {
		return name;
	}
	
	public void addAtribute(String name, String value){
		this.attributes.put(name, value);
	}
	
	
	public Tag(String name, boolean isClosingTag, int id){
		this.name=name;
		this.id=id;
		this.isClosingTag=isClosingTag;
	}
	
	public Tag(String name, boolean isClosingTag){
		this(name, isClosingTag, -1);
	}
	
	public Tag(String name){
		this(name, false, -1);
	}
	
	public Tag(String name, int id){
		this(name, false, id);
	}
	
	
	@Override
	public String toString(){
		if(this.isClosingTag)
			return opener();
		else
			return closer();
	}
	
	public boolean isClosingTag(){
		return isClosingTag;
	}


	/**
	 * Returns a string representation of an opening tag.
	 * It contains the name, id and the other attributes of the Tag Object.
	 * @return
	 */
	public String opener(){
		String rep="<";
		rep+=this.name;
		if(id>=0)
			rep+=" id='"+id+"'";
		for(Entry<String, String> entry: attributes.entrySet()){
			rep+=" "+entry.getKey()+"='"+entry.getValue()+"'";
		}
		rep+=">";
		return rep;
	}
	
	
	/**
	 * Returns a string representation of an closing tag.
	 * It contains the name of the Tag Object.
	 * @return
	 */
	public String closer(){
		return "</"+name+">";
	}
	
	
	/**
	 * Returns true if the tag on which this method is called closes t.
	 * @param t potential closer
	 */
	public boolean isClosing(Tag t){
		if(t==null)
			return false;
		else
			return isClosingTag && !t.isClosingTag && this.name.equals(t.getName());
	}
	
	public boolean isClosedBy(Tag t){
		return !isClosingTag && t.isClosingTag && this.name.equals(t.getName());
	}
}